//
//  MomentDetailViewController.m
//  EconomyDemo
//
//  Created by qianfeng01 on 15-7-8.
//  Copyright (c) 2015年 xiangmu. All rights reserved.
//

#import "MomentDetailViewController.h"
#import "PictureView.h"
#import "AFNetworking.h"
#import "PictureModel.h"
#define kUrl @"http://ipadnews.caijing.com.cn/api/1.0/articles.php?bundleId=cn.com.caijing.android&platform=android4.4.2&appVer=2.4.1&network=wifi&action=detail&id=%d"
@interface MomentDetailViewController ()<UIScrollViewDelegate>
{
    AFHTTPRequestOperationManager *_manager;
    NSMutableArray *_dataArray;
}
@property (nonatomic,strong) AFHTTPRequestOperationManager *manager;
@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,strong) PictureView *scrollerView;
@property (nonatomic,strong) UIPageControl *pageControl;

@end

@implementation MomentDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor=[UIColor blackColor];
    [self loadData];
}
-(void)addScrollView:(NSMutableArray *)imageNames scroll:(NSMutableArray *)scrollNames title:(NSString *)title{
    CGRect frame=CGRectMake(100, 100, self.view.bounds.size.width, 30);
    self.pageControl=[[UIPageControl alloc] initWithFrame:frame];
    self.pageControl.backgroundColor=[UIColor clearColor];
    
    [self.view addSubview:self.pageControl];
    self.scrollerView=[[PictureView alloc] initWithFrame:CGRectMake(0, 150, self.view.bounds.size.width, 400)];
    self.scrollerView.title=title;
    self.scrollerView.delegate=self;
    self.scrollerView.showsHorizontalScrollIndicator=NO;
    self.scrollerView.imageNames=imageNames;
    self.scrollerView.scrollNames=scrollNames;
    [self.view addSubview:self.scrollerView];
    
}
-(void)loadData{
    NSString *url=[NSString stringWithFormat:kUrl,(int)self.articleId.intValue];
    __weak typeof(self) weakSelf=self;
    _manager=[[AFHTTPRequestOperationManager alloc] init];
    _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    [weakSelf.manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"成功下载");
        if (responseObject) {
            NSMutableArray *imageNames=[[NSMutableArray alloc] init];
            NSMutableArray *scrollNames=[[NSMutableArray alloc] init];
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
             PictureModel *model=[[PictureModel alloc] init];
             model.title=dict[@"title"];
             model.path=dict[@"path"];
            NSArray *arr=dict[@"pictures"];
            for (NSDictionary *dic in arr) {
                NSString *imageStr=[NSString stringWithFormat:@"%@%@",model.path,dic[@"fileHD"]];
                NSString *scrollStr=[NSString stringWithFormat:@"%@",dic[@"description"]];
                [imageNames addObject:imageStr];
                [scrollNames addObject:scrollStr];
            }
        
            [weakSelf addScrollView:imageNames scroll:scrollNames title:model.title];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
    }];
    
}

@end
